<?php

namespace app\admin\controller\channel;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\channel\PayOrder;
use app\admin\model\union\Guild;
use app\common\controller\Backend;
use fast\AliyunUtil;

class Agentchannelcz extends Backend
{
    protected $model = null;
    protected $searchFields = '';
    protected $admin_id = null;
    protected $guild_id = null;
    protected $channel = null;
    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->assign('admin_id', $this->admin_id);
        $this->guild_id = Admin::get(intval($auth->id))['guild_id'];
        if($this->guild_id){
            $this->channel = Guild::where('id',$this->guild_id)->value('channel');
        }
        $this->model = new PayOrder();

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $channel = Guild::getGuildChannel($this->guild_id);
        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = json_decode($this->request->get('filter'),true);
            $op = json_decode($this->request->get('op'),true);
            if (isset($op['user_status'])){ //用户状态
//               print_r($filter);
                if ($op['user_status']) $op['uu.status'] = $op['user_status'];
                $filter['uu.status'] = $filter['user_status'];
                unset($op['user_status']);
                unset($filter['user_status']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }
            if (isset($op['role'])){ //认证状态
                if ($op['role']) $op['u.role'] = $op['role'];
                $filter['u.role'] = $filter['role'];
                unset($op['role']);
                unset($filter['role']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }
            if (isset($op['channel'])){ //超推渠道
                if ($op['channel']) $op['uu.channel'] = $op['channel'];
                $filter['uu.channel'] = $filter['channel'];
                unset($op['channel']);
                unset($filter['channel']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where2 = [];
            if($this->channel){ //渠道超推登录
                $where2['uu.channel'] = ['in',explode(',',$this->channel)];
            }else{ //查看所有超推渠道
                $where2['uu.channel'] = ['in',array_keys($channel)];
            }
            //状态：0-有效，1-冻结，2-注销
            $field = "u.user_id,u.user_profile_id,u.role,u.online,u.nick_name,u.avatar,u.charm_value,uu.status as user_status,uu.channel,p.id,p.total_amount,p.finished_at";
            $list = $this->model->field($field)
                ->alias('p')
                ->join('zy_voice_call.t_user_profile u',"u.user_id=p.user_id","LEFT")
                ->join('zy_voice_call.t_user uu',"u.user_id = uu.id","LEFT")
                ->where($where)->where('p.status',1)
                ->where($where2)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list->items() as $v){
                if($v->avatar)$v->avatar= AliyunUtil::buidResUrl($v->avatar,'img');
                $v->total_amount = round($v->total_amount/100, 2);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        //获取所有的渠道信息
//        $channel = AppChannel::getAll();
        $this->assign('channel',$channel);
        return $this->view->fetch();
    }

}